﻿using System;
using System.IO;
using System.Linq.Expressions;

namespace Framework.Core.Recorder
{
    public static class HardMockExtension
    {
        public static void Snapshot<T>(this T target, Stream stream)
        {
            Get<DependencyRecorder, T>(target).Serialize(stream);
        }

        private static TInterceptor Get<TInterceptor, T>(T target)
        {
            try
            {
                return target.Get<TInterceptor>();
            }
            catch (ProxyException e)
            {
                throw new HardMockException(e.Message, e);
            }
        }

        public static void Override<T, R>(this T mock, Expression<Func<T, R>> func, R value)
        {
            Get<DependencyReplayer, T>(mock).Override(func, value);
        }
    }
}